//
//  ViewController.m
//  05-UIScrollView
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
// 宏是系统编译时, 代码的替换(预编译指令)
// 宏定义 命名两种方式
// 1 k + 驼峰命名
#define kScreenBounds [UIScreen mainScreen].bounds
#define kScreenWidth kScreenBounds.size.width
// 2 全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define SUM(a,b) a + b
@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatScrollView];
}

- (void)creatScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    scrollView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:scrollView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 100, 100);
    button.backgroundColor = [UIColor lightGrayColor];
    [button addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:button];
    // 1 默认 CGSizeZero
    NSLog(@"%@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"bounds = %@", NSStringFromCGRect(scrollView.bounds));
    // contentSize 设置可以滚动的范围
    scrollView.contentSize = CGSizeMake(0, 1000);
    // 2 默认 contentOffset 偏移量, 滚动的位置  CGPointZero
    // 不设置contentSize contentOffset也可以改变
#pragma mark - contentOffset
    // 改变contentOffset 实质上是对scrollView的bounds进行修改
    scrollView.contentOffset = CGPointMake(100, 500);
    NSLog(@"%@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"bounds = %@", NSStringFromCGRect(scrollView.bounds));
    // 3 添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 4 锁定垂直或水平滚动(45°角, 会失效)
    scrollView.directionalLockEnabled = YES;
    // 5 回弹效果 默认为YES
    // 边缘弹动动画关闭NO
    scrollView.bounces = YES;
    // 6 alwaysBounceVertical 在没有设置垂直方向滚动范围时, 让垂直方向的回弹效果存在(下拉刷新)
    scrollView.alwaysBounceVertical = YES;
    // 7 alwaysBounceHorizontal 在没有设置水平方向滚动范围时, 让水平方向的回弹效果存在(下拉刷新)
    scrollView.alwaysBounceHorizontal = YES;
    // 8 pagingEnabled 按页滚动
    scrollView.pagingEnabled = YES;
    // 9 滚动视图能否滚动
//    scrollView.scrollEnabled = NO;
    // 10 是否显示垂直/水平的滚动指示器 默认为YES
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    // 11 滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    // 12 滚动指示器的颜色(深色背景, 浅色背景)
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    // 13 相关用户操作
    /*
     用户是否开始拖动
    scrollView.tracking
     用户是否正在拖动
    scrollView.dragging
     是否减速中
    scrollView.decelerating
     */
    // 14 双击状态栏回到顶部
    scrollView.scrollsToTop = YES;
    // 15 代理人
    scrollView.delegate = self;
    // 16 键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 300, 300, 100)];
    textField.backgroundColor = [UIColor redColor];
    textField.placeholder = @"";
    [scrollView addSubview:textField];
    // 17 缩放系数
    // 如果想要进行缩放, 必须实现scrollViewDidEndZooming 协议方法
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 2.0;

}
// 与其想要缩放的子视图绑定, scrollView的contentSize
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    
}

- (void)buttonDidClicked:(UIButton *)button {
    
}
// 滚动视图滚动,就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // tracking 用户触摸
    if (scrollView.isTracking) {
        NSLog(@"滚动中");
    }
    // dragging 用户开始滑动
    if (scrollView.dragging) {
        
    }
    // decelerating 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"");
    }
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
}
// 已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    NSLog(@"结束拖拽");
}
// 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
//    NSLog(@"开始减速");
}
// 已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    NSLog(@"结束减速");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
